package com.hdax.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hdax.entity.CusIntention;
import com.hdax.entity.Customer;
import com.hdax.service.CusIntentionService;
import com.hdax.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 客户意向表 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-03-22
 */
@RestController
@RequestMapping("/cusIntention")
public class CusIntentionController {


    @Autowired
    private CusIntentionService cusIntentionService;
    @Autowired
    private CustomerService customerService;

    @GetMapping("/toIntention")
    public ModelAndView toIntention(){
        return new ModelAndView("intention/intention");
    }


    @PostMapping("/insert")
    public String insertIntention(Customer customer,CusIntention cusIntention){
        customer.setCreateTime(LocalDateTime.now());
        customer.setCustomerState(1);
        customerService.save(customer);
        cusIntention.setIntentionState(1);
        cusIntention.setCusId(customer.getId());
        cusIntention.setCreateTime(LocalDateTime.now());
        cusIntentionService.save(cusIntention);
        return "success";
    }

    @PostMapping("/update")
    public String update(CusIntention cusIntention){
        System.out.println(cusIntention);
        cusIntention.setUpdateTime(LocalDateTime.now());
        cusIntentionService.updateById(cusIntention);
        return "success";
    }

    @PostMapping("/list")
    public IPage<CusIntention> list(Integer page,Integer limit){
        IPage<CusIntention> iPage = new Page<>(page,limit);
        return cusIntentionService.findCusIntention(iPage,new HashMap());
    }

    @GetMapping("/toIntentionInsertAndUpdate")
    public ModelAndView toIntentionInsertAndUpdate(Integer id){
        ModelAndView modelAndView = new ModelAndView("intention/intention-insert-update");
        if(id != null){
            CusIntention cusIntention = cusIntentionService.findCusIntentionById(id);
            System.out.println(cusIntention);
            modelAndView.addObject("cusIntention",cusIntention);
        }
        return modelAndView;
    }

    @PostMapping("/condition")
    public IPage<CusIntention> conditional(@RequestBody Map map){
        IPage<CusIntention> iPage = new Page<>(Long.parseLong(map.get("page").toString()),Long.parseLong(map.get("limit").toString()));
        return cusIntentionService.findCusIntention(iPage,map);
    }






}

